<template>
  <el-card>
    <template #header>
      <div class="card-header">
        <span>房间基本信息管理</span>
      </div>
    </template>
    <el-row class="container" v-for="(item, index) in attrTypeList" :key="index">
      <el-col :span="2" class="text-center">
        <el-popconfirm
          width="220"
          confirm-button-text="删除"
          cancel-button-text="修改"
          cancel-button-type="warning"
          @confirm="deleteAttrHandle(item)"
          @cancel="editAttrHandle(item)"
          :title="`修改或删除${item.name}`"
        >
          <template #reference>
            <div class="item m-r-10 pointer">
              <span>{{ item.name }}</span>
              <el-tooltip effect="dark" content="点击属性名可进行修改/删除操作" placement="top-start">
                <el-icon class="m-l-1"><InfoFilled /></el-icon>
              </el-tooltip>
            </div>
          </template>
        </el-popconfirm>
      </el-col>
      <el-col :span="22" class="item-container">
        <el-popconfirm
          v-for="item2 in item.attrValueList || []"
          :key="item2.id"
          width="220"
          confirm-button-text="删除"
          cancel-button-text="修改"
          cancel-button-type="warning"
          @confirm="deleteRoomBaseHandle(item2)"
          @cancel="editRoomBaseHandle(item2)"
          :title="`修改或删除${item2.name}`"
        >
          <template #reference>
            <div class="item m-r-5 pointer">
              <el-tag style="padding: 13px">{{ item2.name }}</el-tag>
            </div>
          </template>
        </el-popconfirm>
        <el-icon class="item m-r-5 pointer" :size="35" color="#409EFF" @click="addRoomBaseHandle(item)">
          <CirclePlus />
        </el-icon>
      </el-col>
    </el-row>
    <el-button type="primary" class="m-t-5" @click="addAttrHandle"> 添加新属性 </el-button>
    <!--信息修改弹窗管理-->
    <el-dialog v-model="attrKeyDialog.visible" :destroy-on-close="true" :title="attrKeyDialog.title" style="max-width: 700px">
      <el-form ref="attrKeyFormRef" :model="attrKeyData.form" :rules="attrKeyData.rules" label-width="120px">
        <el-form-item label="属性名称" prop="name">
          <el-input v-model.trim="attrKeyData.form.name" autocomplete="off" placeholder="请输入属性名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="attrKeySubmitForm">确 定</el-button>
          <el-button @click="attrKeyCancel">取 消</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 属性信息弹窗管理-->
    <el-dialog v-model="attrValueDialog.visible" :destroy-on-close="true" :title="attrValueDialog.title" style="max-width: 700px">
      <el-form ref="attrValueFormRef" :model="attrValueData.form" :rules="attrValueData.rules" label-width="120px">
        <el-form-item label="属性值名称" prop="name">
          <el-input v-model.trim="attrValueData.form.name" autocomplete="off" placeholder="请输入属性值名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="attrValueSubmitForm">确 定</el-button>
          <el-button @click="attrValueCancel">取 消</el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>
<script setup lang="ts">
import { AttrKeyVO, AttrKeyForm, AttrKeyQuery } from '@/api/apartment/attribute/attrKey/types';
import { AttrValueVO, AttrValueForm, AttrValueQuery } from '@/api/apartment/attribute/attrValue/types';
import { delAttrKey, listAttrKey, updateAttrKey, addAttrKey, getAttrKey } from '@/api/apartment/attribute/attrKey/index';
import { delAttrValue, getAttrValue, updateAttrValue, addAttrValue } from '@/api/apartment/attribute/attrValue/index';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const buttonLoading = ref(false);
const attrKeyFormRef = ref<ElFormInstance>();
const attrValueFormRef = ref<ElFormInstance>();
const attrKeyDialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const attrValueDialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const initAttrKeyFormData: AttrKeyForm = {
  id: undefined,
  name: undefined
};
const attrKeyData = reactive<PageData<AttrKeyForm, AttrKeyQuery>>({
  form: { ...initAttrKeyFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 500,
    name: undefined,
    params: {}
  },
  rules: {
   name: [{ required: true, message: '属性名称不能为空', trigger: 'blur' }]
  }
});
const initAttrValueFormData: AttrValueForm = {
  id: undefined,
  name: undefined,
  attrKeyId: undefined,
  attrKeyName: undefined
};
const attrValueData = reactive<PageData<AttrValueForm, AttrValueQuery>>({
  form: { ...initAttrValueFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    attrKeyId: undefined,
    params: {}
  },
  rules: {
    name: [{ required: true, message: '属性值名称不能为空', trigger: 'blur' }]
  }
});
// 属性类型
const attrTypeList = ref<AttrKeyVO[]>([]);
// 获取属性信息
const getRoomBaseInfoListHandle = async () => {
  const { queryParams, form, rules } = toRefs(attrKeyData);
  const { data } = await listAttrKey(queryParams.value);
  data.forEach((item) => {
    item.attrValueList?.forEach((item2) => {
      item2.attrKeyName = item.name;
    });
  });
  attrTypeList.value = data;
};
// 删除属性值
const deleteRoomBaseHandle = async (item: AttrValueVO) => {
  await delAttrValue(item.id);
  await getRoomBaseInfoListHandle();
  proxy?.$modal.msgSuccess('操作成功');
};
// 编辑属性值
const editRoomBaseHandle = async (item: AttrValueVO) => {
  const { form } = toRefs(attrValueData);
  attrValueReset();
  const _id = item?.id;
  const res = await getAttrValue(_id);
  Object.assign(form.value, res.data);
  attrValueDialog.visible = true;
  attrValueDialog.title = '修改房间' + item.name + '属性值';
};
// 添加属性值
const addRoomBaseHandle = (item: AttrKeyVO) => {
  attrValueReset();
  attrValueDialog.visible = true;
  attrValueDialog.title = '添加房间' + item.name + '属性值';
  const { form } = toRefs(attrValueData);
  form.value.attrKeyId = item.id;
  form.value.attrKeyName = item.name;
};
// 添加新属性
const addAttrHandle = () => {
  attrKeyReset();
  attrKeyDialog.visible = true;
  attrKeyDialog.title = '添加房间基本属性';
};
// 删除属性
const deleteAttrHandle = async (item: AttrKeyVO) => {
  await delAttrKey(item.id);
  await getRoomBaseInfoListHandle();
  proxy?.$modal.msgSuccess('操作成功');
};
// 编辑属性
const editAttrHandle = async (item: AttrKeyVO) => {
  const { form } = toRefs(attrKeyData);
  attrKeyReset();
  const _id = item?.id;
  const res = await getAttrKey(_id);
  Object.assign(form.value, res.data);
  attrKeyDialog.visible = true;
  attrKeyDialog.title = '修改房间基本属性';
};
/** 提交按钮 */
const attrKeySubmitForm = () => {
  const { queryParams, form, rules } = toRefs(attrKeyData);
  attrKeyFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateAttrKey(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addAttrKey(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      attrKeyDialog.visible = false;
      await getRoomBaseInfoListHandle();
    }
  });
};
/** 取消按钮 */
const attrKeyCancel = () => {
  attrKeyReset();
  attrKeyDialog.visible = false;
};
/** 表单重置 */
const attrKeyReset = () => {
  const { form } = toRefs(attrKeyData);
  form.value = { ...initAttrKeyFormData };
  attrKeyFormRef.value?.resetFields();
};

/** 提交按钮 */
const attrValueSubmitForm = () => {
  const { queryParams, form, rules } = toRefs(attrValueData);
  attrValueFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateAttrValue(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addAttrValue(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      attrValueDialog.visible = false;
      await getRoomBaseInfoListHandle();
    }
  });
};
/** 取消按钮 */
const attrValueCancel = () => {
  attrValueReset();
  attrValueDialog.visible = false;
};
/** 表单重置 */
const attrValueReset = () => {
  const { form } = toRefs(attrValueData);
  form.value = { ...initAttrValueFormData };
  attrValueFormRef.value?.resetFields();
};

onMounted(() => {
  getRoomBaseInfoListHandle();
});
</script>

<style scoped lang="scss">
.card-header {
  font-size: 18px;
  font-weight: bold;
}

.text-center {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  text-align: center;
}

.container:not(:first-child) {
  margin-top: 20px;
}

.item-container {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
  padding: 10px 15px;
  background-color: #efefef;
  border-radius: 20px;

  .item {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }
}
</style>
